package com.wjc.train.business.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjc.train.business.entity.TrainCarriage;
import com.wjc.train.business.entity.TrainSeat;
import com.wjc.train.business.enums.SeatColEnum;
import com.wjc.train.business.repository.TrainSeatRepository;
import com.wjc.train.business.req.TrainSeatQueryReq;
import com.wjc.train.business.req.TrainSeatSaveReq;
import com.wjc.train.business.resp.TrainSeatQueryResp;
import com.wjc.train.business.service.ITrainCarriageService;
import com.wjc.train.business.service.ITrainSeatService;
import com.wjc.train.common.exception.BusinessException;
import com.wjc.train.common.untils.CopyUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 座位表 服务实现类
 * </p>
 *
 * @author Guessed
 * @since 2023-08-03
 */
@Service
public class TrainSeatServiceImpl implements ITrainSeatService {

    @Resource
    private TrainSeatRepository trainseatRepository;

    @Resource
    private ITrainCarriageService trainCarriageService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(TrainSeatSaveReq trainseatSaveReq) {
        TrainSeat trainseat = CopyUtil.copy(trainseatSaveReq, TrainSeat.class);
        trainseatRepository.saveOrUpdate(trainseat);
    }

    @Override
    public IPage<TrainSeatQueryResp> queryList(IPage<TrainSeatQueryResp> page, TrainSeatQueryReq trainseatQueryReq) {
        return trainseatRepository.queryList(page, trainseatQueryReq);
    }

    @Override
    public void delete(Long id) {
        trainseatRepository.getBaseMapper().deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void genTrainSeat(String trainCode) {
        //不能重复生成,先删除再生成
        trainseatRepository.getBaseMapper().deleteRealById(trainCode);
        //查询当前车次下的所有车厢
        List<TrainCarriage> trainCarriages
                = trainCarriageService.queryByTrainCode(trainCode);
        if (CollectionUtil.isEmpty(trainCarriages)) {
            throw new BusinessException("该车次车厢为空");
        }

        List<TrainSeat> insertList = new ArrayList<>();
        //循环生成每个车厢的座位
        for (TrainCarriage carriage : trainCarriages) {
            //初始化车厢index
            int seatIndex = 1;
            //拿到车厢的数据，行数,座位类型等
            Integer rowCount = carriage.getRowCount();
            Integer seatType = carriage.getSeatType();
            //根据座位类型，筛选出所有的列
            List<SeatColEnum> seatColEnumList = SeatColEnum.getColsByType(seatType);
            //循环行数
            for (int row = 1; row <= rowCount; row++) {
                //循环列数
                for (SeatColEnum seatColEnum : seatColEnumList) {
                    //构造座位数据
                    TrainSeat trainSeat = new TrainSeat();
                    trainSeat.setTrainCode(trainCode);
                    trainSeat.setSeatType(seatType);
                    //在字符串前面填充0
                    trainSeat.setRow(StrUtil.fillBefore(String.valueOf(row), '0', 2));
                    trainSeat.setCol(seatColEnum.getCode());
                    trainSeat.setCarriageIndex(carriage.getIndex());
                    trainSeat.setCarriageSeatIndex(seatIndex++);
                    insertList.add(trainSeat);
                }
            }
        }
        if(CollectionUtil.isNotEmpty(insertList)){
            trainseatRepository.saveBatch(insertList);
        }
    }
}
